﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ControlsLib.Filters;
using GestionePolveriLib.BL.FilterBags;
using GestionePolveriLib.Utilities;
using GestionePolveriLib.BL;
using GestionePolveriLib.Facades;

namespace GestionePolveri2.Filters
{
    public partial class AnagraficaPolveriFilter : AnagraficaPolveriFilterBase
    {
        public AnagraficaPolveriFilter()
        {
            InitializeComponent();

            if (!this.DesignMode)
            {
                RefreshData();

                this.FromDate = DateTime.Now;
                this.ToDate = DateTime.Now;

                this.EnableDatesChooser = false;
            }
        }

        protected override void OnResetFilter(EventArgs e)
        {
            base.OnResetFilter(e);

            this.cmbColori.SelectedIndex = 0;
            this.cmbPolveri.SelectedIndex = 0;
            this.cmbFornitori.SelectedIndex = 0;

            FilterBag.CodiceColore = null;
            FilterBag.CodicePolvere = null;
            FilterBag.Fornitore = null;
        }

        protected override void OnExecuteFilter(EventArgs e)
        {
            if (this.cmbPolveri.SelectedItem == null)
                this.cmbPolveri.SelectedIndex = 0;
            if (this.cmbColori.SelectedItem == null)
                this.cmbColori.SelectedIndex = 0;
            if (this.cmbFornitori.SelectedItem == null)
                this.cmbFornitori.SelectedIndex = 0;

            FilterBag.CodiceColore = (((Colore)this.cmbColori.SelectedItem).ID == -1 ? null : ((Colore)this.cmbColori.SelectedItem).CodiceColore);
            FilterBag.CodicePolvere = (((Polvere)this.cmbPolveri.SelectedItem).ID == -1 ? null : ((Polvere)this.cmbPolveri.SelectedItem).CodicePolvere);
            FilterBag.Fornitore = (((Fornitore)this.cmbFornitori.SelectedItem).ID == -1 ? null : ((Fornitore)this.cmbFornitori.SelectedItem).Descrizione);

            if (
                (this.cmbColori.SelectedIndex == 0) &&
                (this.cmbPolveri.SelectedIndex == 0) &&
                (FilterBag.CodiceColore == null) &&
                (FilterBag.Fornitore == null) &&
                (FilterBag.CodicePolvere == null) 
              )
            {
                OnResetFilter(e);
                this.OnFilterButtonClick();
            }
            else
            {
                base.OnExecuteFilter(e);
            }
        }

        private void LoadPolveri()
        {
            FilterComboBoxHelper.SetupPolveriComboBox(this.cmbPolveri);
        }

        private void LoadColori()
        {
            FilterComboBoxHelper.SetupColoriComboBox(this.cmbColori);
        }

        private void LoadFornitori()
        {
            FilterComboBoxHelper.SetupFornitoriComboBox(this.cmbFornitori);
        }

        private void LoadFamiglie()
        {
            FamigliaColoriFacade pf = new FamigliaColoriFacade();

            pf.RunAsync(() => pf.GetAll(), (result) =>
            {
                if (result != null)
                {
                    this.chkCmbFamiglia.Properties.DisplayMember = "Descrizione";
                    this.chkCmbFamiglia.Properties.ValueMember = "ID";
                    this.chkCmbFamiglia.Properties.DataSource = result;
                }
            });
        }

        public override void RefreshData()
        {
            LoadPolveri();
            LoadColori();
            LoadFornitori();
            LoadFamiglie();
        }
    }


    public class AnagraficaPolveriFilterBase : FilterContainerBase2<AnagraficaPolveriFilterBag> { }
}
